package net.pessu.android.shoppoid.remise;

import greendroid.app.GDActivity;
import greendroid.widget.ActionBar;
import greendroid.widget.ActionBarItem;
import net.pessu.android.shoppoid.R;
import net.pessu.android.shoppoid.bean.Article;
import net.pessu.android.shoppoid.gui.ButtonListLayout;
import net.pessu.android.shoppoid.gui.ButtonListRemise;
import net.pessu.android.shoppoid.gui.IhmObjets;
import net.pessu.android.shoppoid.gui.TextSolution;
import net.pessu.android.shoppoid.wishlist.AddArticle;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class Remise extends GDActivity {
	
	private static final String TAG = "Remise";

	EditText input;
	TextSolution solution;
	ButtonListRemise buttonActif;
	// List<ButtonRemise> listButtonRemise;
	ButtonListLayout buttonRemiseLayout;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setActionBarContentView(R.layout.calculette);
		ActionBar bar = getActionBar();
		bar.setTitle(getString(R.string.remise));
		bar.setFont(IhmObjets.tfTitre);
		bar.setFontSize(30);
		input = (EditText) findViewById(R.id.input1);
		solution = (TextSolution) findViewById(R.id.solution);
		Button reductionSupButton = (Button) findViewById(R.id.reReducbutton);
		Button addtolistButton = (Button) findViewById(R.id.addtolist);

		buttonRemiseLayout = (ButtonListLayout) findViewById(R.id.buttonLayout);

		((ButtonListRemise) findViewById(R.id.cinq)).init(0.95, input,
				solution, buttonRemiseLayout);
		((ButtonListRemise) findViewById(R.id.dix)).init(0.90, input, solution,
				buttonRemiseLayout);
		((ButtonListRemise) findViewById(R.id.quinze)).init(0.85, input,
				solution, buttonRemiseLayout);
		((ButtonListRemise) findViewById(R.id.vingt)).init(0.80, input,
				solution, buttonRemiseLayout);
		((ButtonListRemise) findViewById(R.id.vingtcinq)).init(0.75, input,
				solution, buttonRemiseLayout);
		((ButtonListRemise) findViewById(R.id.trente)).init(0.70, input,
				solution, buttonRemiseLayout);
		((ButtonListRemise) findViewById(R.id.trentecinq)).init(0.65, input,
				solution, buttonRemiseLayout);
		((ButtonListRemise) findViewById(R.id.quarante)).init(0.60, input,
				solution, buttonRemiseLayout);
		((ButtonListRemise) findViewById(R.id.quarantecinq)).init(0.55, input,
				solution, buttonRemiseLayout);
		((ButtonListRemise) findViewById(R.id.cinquante)).init(0.50, input,
				solution, buttonRemiseLayout);
		((ButtonListRemise) findViewById(R.id.cinquantecinq)).init(0.45, input,
				solution, buttonRemiseLayout);
		((ButtonListRemise) findViewById(R.id.soixante)).init(0.40, input,
				solution, buttonRemiseLayout);
		((ButtonListRemise) findViewById(R.id.soixantecinq)).init(0.35, input,
				solution, buttonRemiseLayout);
		((ButtonListRemise) findViewById(R.id.soixantedix)).init(0.30, input,
				solution, buttonRemiseLayout);
		((ButtonListRemise) findViewById(R.id.soixantequinze)).init(0.25,
				input, solution, buttonRemiseLayout);
		((ButtonListRemise) findViewById(R.id.quatrevingt)).init(0.20, input,
				solution, buttonRemiseLayout);
		((ButtonListRemise) findViewById(R.id.quatrevingtcinq)).init(0.15,
				input, solution, buttonRemiseLayout);
		((ButtonListRemise) findViewById(R.id.quatrevingtdix)).init(0.10,
				input, solution, buttonRemiseLayout);
		((ButtonListRemise) findViewById(R.id.quatrevingtquinze)).init(0.05,
				input, solution, buttonRemiseLayout);

		reductionSupButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				if (solution != null && solution.getText() != null) {
					input.setText(solution.getSolutionText());
					solution.setText("");
					buttonRemiseLayout.initializeButtons();
				}
			}
		});

		addtolistButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {

				if (solution.getText().length() > 0) {

					Article article = new Article();
					article.setPrixreduit(solution.getSolution());
					article.setTauxReduc(getTauxAppliquer());
					
					double entre = 0;

					try {
						entre = Double.parseDouble(input.getText().toString());
					} catch (NumberFormatException nfe) {
						Log.e(TAG, nfe.toString());
					}
					article.setPrixOrigine(entre);

					showArticleAdder(article);
					finish();
				}
			}
		});

	}

	@Override
	public boolean onHandleActionBarItemClick(ActionBarItem item, int position) {
		switch (item.getItemId()) {
		default:
			return super.onHandleActionBarItemClick(item, position);
		}
	}

	/**
	 * Launches the wishlist activity to add a new contact to the selected
	 * accont.
	 */
	protected void showArticleAdder(Article article) {
		Intent i = new Intent(this, AddArticle.class);
		i.putExtra("article", article);
		startActivity(i);
	}

	private double getTauxAppliquer() {
		return buttonRemiseLayout.getSelectedButtonTaux();
	}

}